package znbkTopicType.resContent;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.lancooznbk.R;

import znbkTopicType.WrapBaseAdapter;
import znbkTestPaperInfor.ZnbkPaperDef;

import static znbkTestPaperBj.activity.NotePaperActivity.mEntityBj;
import static znbkTestPaperRdtz.activity.HeatPointExtensionPaperActivity.mEntityRdtz;
import static znbkTestPaperSc.activity.CollectionPaperActivity.mEntitySc;

/**
 * Created by Administrator on 2018\3\18 0018.
 */

public class ZnbkContentResStlZwAdapter extends WrapBaseAdapter {

    private LayoutInflater mInflater;
    private Context mContext;
    //当前试题数量
    private int SumItem;
    //当前试题在当前试卷的ID
    private int PaperID;
    private int ModelFrom;//试卷来源

    private int tvBlack;
    private int tvBlue;
    private int tvWhite;
    private int tvGreen;
    private int tvRed;


    private ViewHolder holder = null;
    private class ViewHolder{
        WebView WvTheme;//大题文本题干区
        LinearLayout mLayoutStuAnswer;//我的答案显示区
        LinearLayout mLayoutTvStuAnswer;//我的答案文字显示
        LinearLayout mLayoutAnswer;//正确答案显示区
        LinearLayout mLayoutTvAnswer;//正确答案文字显示
        LinearLayout mLayoutStuAssess;//自评显示区
        TextView mTvStuAssess;//自评文字显示
        LinearLayout mLayoutAnalysis;//解析显示区
        TextView mTvAnalysis;//解析文字显示


        private ViewHolder(View convertView) {
            WvTheme = (WebView) convertView.findViewById(R.id.wv_theme);
            mLayoutStuAnswer = (LinearLayout) convertView.findViewById(R.id.layout_stuAnswer);
            mLayoutTvStuAnswer = (LinearLayout) convertView.findViewById(R.id.layout_tvStuAnswer);
            mLayoutAnswer = (LinearLayout) convertView.findViewById(R.id.layout_answer);
            mLayoutTvAnswer = (LinearLayout) convertView.findViewById(R.id.layout_tvAnswer);
            mLayoutStuAssess = (LinearLayout) convertView.findViewById(R.id.layout_stuAssess);
            mTvStuAssess = (TextView) convertView.findViewById(R.id.tv_stuAssess);
            mLayoutAnalysis = (LinearLayout) convertView.findViewById(R.id.layout_analysis);
            mTvAnalysis = (TextView) convertView.findViewById(R.id.tv_analysis);

        }
    }

    /*
   * 获取小题选项信息
   * */
    public ZnbkContentResStlZwAdapter(Context context, int sumItem, int paperID, int modelFrom) {
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.SumItem = sumItem;
        this.PaperID = paperID;
        this.ModelFrom = modelFrom;

        tvBlack = mContext.getResources().getColor(R.color.black);
        tvBlue = mContext.getResources().getColor(R.color.blue);
        tvWhite = mContext.getResources().getColor(R.color.white);
        tvGreen = mContext.getResources().getColor(R.color.green);
        tvRed = mContext.getResources().getColor(R.color.red);
    }

    @Override
    public int getCount() {
        return SumItem;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.znbk_adapter_content_res_zw_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        /*
        * 获取作文题用户相关信息
        * */
        getQuesInfor();

        /*
        * 显示题干信息
        * */
        showQuesInfor();

        /*
        * 显示解析内容
        * */
        showAnalysis();


        return convertView;
    }


    /*
    * 获取作文题用户相关信息
    * */
    private String SubChildAsk = "";
    private String SubChildAnswer;
    private String SubChildAnalysis;
    private void  getQuesInfor(){

        if(ModelFrom== ZnbkPaperDef.FROM_HEAT_POINT_EXTENSION) {


                /*
                * 题干内容：SubChildAsk
                * */
                    SubChildAsk = mEntityRdtz.getResFromTK().getChildList().get(0).getSubChildList().get(0).getSubChildAsk();

                /*
                * 正确答案：SubChildAnswer
                * */
                    SubChildAnswer = mEntityRdtz.getResFromTK().getChildList().get(0).getSubChildList().get(0).getSubChildAnswer();

                /*
                * 答案解析：SubChildAnalysis
                * */
                    SubChildAnalysis = mEntityRdtz.getResFromTK().getChildList().get(0).getSubChildList().get(0).getSubChildAnalyze();

        }else if(ModelFrom== ZnbkPaperDef.FROM_COLLECTION) {


                /*
                * 题干内容：SubChildAsk
                * */
                SubChildAsk = mEntitySc.getResDetail().getChildList().get(0).getSubChildList().get(0).getSubChildAsk();

                /*
                * 正确答案：SubChildAnswer
                * */
                SubChildAnswer = mEntitySc.getResDetail().getChildList().get(0).getSubChildList().get(0).getSubChildAnswer();

                /*
                * 答案解析：SubChildAnalysis
                * */
                SubChildAnalysis = mEntitySc.getResDetail().getChildList().get(0).getSubChildList().get(0).getSubChildAnalyze();

        }else if(ModelFrom== ZnbkPaperDef.FROM_NOTE) {


                /*
                * 题干内容：SubChildAsk
                * */
            SubChildAsk = mEntityBj.getModelTKQue().getChildList().get(0).getSubChildList().get(0).getSubChildAsk();

                /*
                * 正确答案：SubChildAnswer
                * */
            SubChildAnswer = mEntityBj.getModelTKQue().getChildList().get(0).getSubChildList().get(0).getSubChildAnswer();

                /*
                * 答案解析：SubChildAnalysis
                * */
            SubChildAnalysis = mEntityBj.getModelTKQue().getChildList().get(0).getSubChildList().get(0).getSubChildAnalyze();

        }


    }

    /*
    * 显示界面信息
    * */
    private void showQuesInfor(){
        /*
        * 显示题干内容
        * */
        holder.WvTheme.loadDataWithBaseURL("about:blank",SubChildAsk,"text/html","utf-8",null);
    }

    /*
    * 显示解析内容
    * */
    private void showAnalysis(){
        /*
        * 隐藏我的答案
        * */
        holder.mLayoutStuAnswer.setVisibility(View.GONE);
        /*
        * 显示参考答案
        * */
        holder.mLayoutAnswer.setVisibility(View.VISIBLE);
        TextView mTvAnswer = new TextView(mContext);
        mTvAnswer.setText(Html.fromHtml(SubChildAnswer));
        mTvAnswer.setTextColor(tvGreen);
        holder.mLayoutTvAnswer.addView(mTvAnswer);
        /*
        * 隐藏自评信息
        * */
        holder.mLayoutStuAssess.setVisibility(View.GONE);
        /*
        * 显示解析信息
        * */
        holder.mLayoutAnalysis.setVisibility(View.VISIBLE);
        if(SubChildAnalysis.equals("")){
            holder.mTvAnalysis.setText(R.string.znbk_no_analysis);
        }else {
            holder.mTvAnalysis.setText(SubChildAnalysis);
        }
        holder.mTvAnalysis.setTextColor(tvBlack);
    }

}